package com.zretc.support.dao;

import com.zretc.support.entity.Answer;
import com.zretc.support.entity.Exam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Repository
public interface ExamDao extends JpaRepository<Exam, String>, JpaSpecificationExecutor<Exam>,
        CrudRepository<Exam, String>, Serializable {
    /**
     * 精确查找
     *
     * @param keywords
     * @return
     */
    @Query(value = "select * from Exam  where examName = ?1", nativeQuery = true)
    List<Exam> getAllByExamName(String keywords);
    /**
     * 精确查找
     *
     * @param examId
     * @return
     */
    @Query(value = "select * from Exam  where examId = ?1", nativeQuery = true)
    Exam getAllByExamId(long examId);

    /**
     * 分页+模糊查找
     *
     * @param keywords
     * @param pageable
     * @return
     */
    @Query(value = "select * from Exam  where examName like %?1%", nativeQuery = true)
    Page<Exam> page(String keywords, Pageable pageable);

    /**
     * 删除方法
     *
     * @param examId
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "delete from Exam  where examId = ?1", nativeQuery = true)
    int deleteByExamId(long examId);

    /**
     * 添加方法
     * @param examName
     * @param examBegin
     * @param examTime
     * @param paperId
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "insert into Exam values(null,?1,?2,?3,?4)", nativeQuery = true)
    int add(String examName, Date examBegin, long examTime, long paperId);

    /**
     * 前台 学生 我的考试 考试信息 分页
     * @param studentId
     * @param pageable
     * @return
     */
    @Query("select e from Joincourse j inner join Exampaper ep on j.course.courseId = ep.course.courseId " +
            "inner join Exam e on e.exampaper.paperId = ep.paperId where j.user.userId=?1 and e.examName like %?2%")
    Page<Exam> getExamsByUserId(String studentId,String keywords,Pageable pageable);
}
